<?php
//咨询控制器
class Controller_WWW_Chat extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/tourist/chat/chat";
        parent::__construct($req, $res);
    }

	//咨询信息展示
    public function action_with ($toUid)
    {
        $chatList = App_Chat::infoList($this->_user,$toUid);
//        var_dump($chatList);exit;
        $user = User::getUserFromUid($toUid);
        $userInfo = $user->getUserInfo();
        $this->template->set_filename('smarty:www/tourist/chat/chat');
        $this->template->set('chatList', $chatList);
        $this->template->set('userInfo', $userInfo);
        $this->template->set('uid', $toUid);
    }

     public function action_refresh()
    {
    	$this->_post = $this->request->post();
    	$toUid = (int) Arr::get($this->_post, 'uid');
    	$limit = (int) Arr::get($this->_post, 'limit');
    	$offset = (int) Arr::get($this->_post, 'offset');
    	$cache = Cache::instance('file');
    	$key = $toUid.'-'.$this->_uid;
    	$unread = $cache->get($key);
    	$unread['total'] = 0;
    	$cache->set($key,$unread);
    	$chatList = App_Chat::infoList($this->_user,$toUid,$offset,$limit);
    	$html     = View::factory('smarty:app/chat/records')
    	->set('chatList', $chatList)
    	->render();
    	
    	$ret = array('status' => 0, 'html' => $html);
    	echo json_encode($ret);exit;
    }

	//接受并处理提交到服务器的信息
    public function action_api ()
    {
        $img = Arr::get($this->_post, 'img');
        $img = trim($img);
        $txt = Arr::get($this->_post, 'txt');
        $txt = trim($txt);
        if (! empty($txt))
        {
            $txt = htmlspecialchars($txt);
            $txt = nl2br($txt);
        }
        if(stripos($txt,'#') !== FALSE){
        $search =array(
        		"#呵呵#", "#哈哈#","#吐舌#", "#啊#","#酷#","#怒#", "#开心#", "#汗#", "#泪#", "#黑线#", 
        		"#鄙视#", "#不高兴#", "#真棒#", "#钱#", "#疑问#", "#阴脸#", "#吐#", "#咦#", "#委屈#", 
        		"#花心#", "#呼~#", "#笑脸#", "#冷#", "#太开心#", "#滑稽#", "#勉强#", "#狂汗#", "#乖#", 
        		"#睡觉#", "#惊哭#", "#生气#", "#惊讶#", "#喷#", "#爱心#", "#心碎#", "#玫瑰#", "#礼物#", 
        		"#彩虹#", "#星星月亮#", "#太阳#", "#钱币#", "#灯泡#","#茶杯#", "#蛋糕#", "#音乐#", 
        		"#haha#","#胜利#","#大拇指#", "#弱#","#OK#");
        foreach($search as $k=>$v){
        	$k++;
                $txt= str_replace($v, "<img src=\"/resource/skin/face/tieba/".$k.".png\" />", $txt);
        }
        }
        $toUid = Arr::get($this->_post, 'touid');
        $guiderId = Arr::get($this->_post,'uid');

        $cache = Cache::instance('file');
        $key = $this->_uid.'-'.$guiderId;
        $data = $cache->get($key);
        if(empty($data)){
            $data['total'] = 0;
            $data['msg'] = '';
            $data['time'] = time();
            $data['last'] = time();
        }

        //对方不在线上次输入和本次输入相差1h以上提醒
        $dif=time()-Arr::get($data,'time');
        $dif=abs($dif);
        if(!Util_User::is_online($toUid)&&$dif>3600){
            Util_Notice::notice_message($toUid,$this->_uid,'GUIDER');
        }

        $cache->set($key,$data);

        if (! empty($img))
            $img = "<img src=\"$img\">";

        if (empty($txt) && empty($img))
            exit;

        $txt   = empty($img) ? $txt : $img;
        if ($toUid > 0)
        {
            $user  = User::getUserFromUid($toUid);
            $msgId = App_Chat::add($user, array('message' => $txt, 'fromUser' => 0,'guiderId'=>$guiderId));
        }
        else
        {
            $data['total'] += 1;
            $data['msg'] = $txt;
            $data['last'] = Arr::get($data,'time',time());;
            $data['time'] = time();
            $cache->set($key,$data,86400*10);
            $msgId = App_Chat::add($this->_user, array('message' => $txt, 'fromUser' => 1,'guiderId'=>$guiderId));
        }

        //$html  = App_Chat::buildMessageHtml($msgId);
            
        echo json_encode(array(
            'status' => 0,
           // 'html'   => $html,
        ));

        exit;
    }

    public function action_list ()
    {
        $addrList = App_Address::infoList($this->_user);
        $this->template->set_filename('smarty:app/address/list');
        $this->template->set('addressList', $addrList);
    }

    public function action_add ()
    {
        if (empty($this->_post))
        {
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            
        }
        else
        {
            App_Address::add($this->_user, $this->_post);
            $this->request->redirect('/address/list');
        }
    }

    public function action_edit ($nid)
    {
        if (empty($this->_post))
        {
            $addrInfo = App_Address::detail($nid);
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            $this->template->set('optionsCity', App_Region::optionsByParentId($addrInfo['proviceId']));
            $this->template->set('optionsCountry', App_Region::optionsByParentId($addrInfo['cityId']));
            $this->template->set('address', $addrInfo);
        }
        else
        {
            App_Address::edit($this->_user, $nid, $this->_post);
            $this->request->redirect('/address/list');
        }
    }

    public function action_del ($nid)
    {
        App_Address::remove($this->_user, $nid);
        $this->request->redirect('/address/list');
    }
}
